package com.gateon.hw.yunding.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.gateon.hw.yunding.model.Params;
import com.gateon.hw.yunding.model.Urls;
import com.gateon.hw.yunding.model.repuest.LoginRequest;
import com.gateon.hw.yunding.service.MsgWriter;
import com.gateon.hw.yunding.service.SoundService;
import io.netty.util.internal.StringUtil;
import lombok.experimental.Accessors;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 设备功能总控实现类
 *
 * @program: hw
 * @description: qiuheyi
 * @author: qhy
 * @create: 2020-08-21 13:58
 **/
@Service
@Accessors(chain = true)
public class SoundServiceImpl implements SoundService {

    /**
     * 订阅主题名
     */
    private static final String source = "test_mqtt_client_3";

    @Autowired
    private MsgWriter msgWriter;

    @Autowired
    private RedisTemplate redisTemplate;

    public LoginRequest loginRequest() {
        LoginRequest loginRequest = new LoginRequest();
        loginRequest.setType("req");
        loginRequest.setCallid(1166556110);
        loginRequest.setApp("ls20");
        return loginRequest;
    }

    @Override
    public String sing(String name, String password) throws MqttException {
        LoginRequest loginRequest = loginRequest();
        loginRequest.setOp("login");
        Params params = new Params();
        params.setName(name);
        params.setPassword(password);
        params.setReplace(0L);
        params.setSource(source);
        loginRequest.setParams(params);
        String msg = JSON.toJSONString(loginRequest);
        msgWriter.sendToMqtt(msg);

        String respMsg = (String) redisTemplate.opsForValue().get(source);

        //转实体
//        LoginResponse loginResponse = (LoginResponse) JSON.parse(respMsg);
        return respMsg;

    }

    @Override
    public String getAllDevices(String uid) throws MqttException {
        LoginRequest loginRequest = loginRequest();
        loginRequest.setOp("getAllDevices");
        Params params = new Params();
        params.setSource(source);
        params.setUid(uid);
        loginRequest.setParams(params);
        String msg = JSON.toJSONString(loginRequest);
        msgWriter.sendToMqtt(msg);

        String respMsg = (String) redisTemplate.opsForValue().get(source);

        return respMsg;
    }

    @Override
    public String addTask(String uid, String name, String[] devices, List<Urls> urls, Long vol) throws MqttException {
        LoginRequest loginRequest = loginRequest();
        loginRequest.setOp("addTask");
        Params params = new Params();
        params.setType(4L);
        params.setSource(source);
        params.setUid(uid);
        params.setName(name);
        params.setVol(vol);
        params.setDevices(devices);
        params.setUrls(urls);
        loginRequest.setParams(params);
        String msg = JSON.toJSONString(loginRequest);
        msgWriter.sendToMqtt(msg);
        String respMsg = (String) redisTemplate.opsForValue().get(source);
        return respMsg;
    }

    @Override
    public void delTask(String uid, String tid) throws MqttException {
        LoginRequest loginRequest = loginRequest();
        loginRequest.setOp("delTask");
        Params params = new Params();
        params.setSource(source);
        params.setUid(uid);
        params.setTid(tid);
        loginRequest.setParams(params);
        String msg = JSON.toJSONString(loginRequest);
        msgWriter.sendToMqtt(msg);
    }

    @Override
    public String playTask(String uid, String tid) throws MqttException {
        LoginRequest loginRequest = loginRequest();
        loginRequest.setOp("playTask");
        Params params = new Params();
        params.setSource(source);
        params.setUid(uid);
        params.setTid(tid);
        loginRequest.setParams(params);
        String msg = JSON.toJSONString(loginRequest);
        msgWriter.sendToMqtt(msg);

        String respMsg = (String) redisTemplate.opsForValue().get(source);
        return respMsg;
    }

    @Override
    public String stopTask(String uid, String tid) throws MqttException {
        LoginRequest loginRequest = loginRequest();
        loginRequest.setOp("stopTask");
        Params params = new Params();
        params.setSource(source);
        params.setUid(uid);
        params.setTid(tid);
        loginRequest.setParams(params);
        String msg = JSON.toJSONString(loginRequest);
        msgWriter.sendToMqtt(msg);

        String respMsg = (String) redisTemplate.opsForValue().get(source);
        return respMsg;
    }
}
